package com.hans.scala


/**
 * ClassTag test
 * Created by Hans on 2014/4/13.
 */
object ClassTagExample {
  def main(args: Array[String]) {
    //ClassTag Manifest
    mkArray(42, 13)
    mkArray("Japan", "Brazil", "Germany")
    mkArray1("Japan", "Brazil", "Germany")

    //TypeTag
    paramInfo(42)
    paramInfo(List(1, 2))

    //WeakTypeTag
    def foo[T]() = weakParamInfo(List[T]())
    foo[Int]()
  }

  import scala.reflect.ClassTag

  def mkArray[T: Manifest](elems: T*) = Array[T](elems: _*)

  import scala.reflect.runtime.universe._

  def mkArray1[T: ClassTag](elems: T*) = Array[T](elems: _*)

  def paramInfo[T: TypeTag](x: T): Unit = {
    val targs = typeOf[T] match {
      case TypeRef(_, _, args) => args
    }
    println(s"type of $x has type arguments $targs")
  }

  def weakParamInfo[T](x: T)(implicit tag: WeakTypeTag[T]): Unit = {
    val targs = tag.tpe match {
      case TypeRef(_, _, args) => args
    }
    println(s"type of $x has type arguments $targs")
  }

}
